package com.xxkj.nmip.page;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.xxkj.nmip.R;
import com.xxkj.nmip.dialog.InputDialog;
import com.xxkj.nmip.js.JsApi;
import com.xxkj.nmip.js.JsEchoApi;

import wendu.dsbridge.DWebView;

public class JavascriptCallNativeActivity extends AppCompatActivity {
    Button bt_change_url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_js_call_native);
         DWebView dwebView= (DWebView) findViewById(R.id.webview);
        // set debug mode
        DWebView.setWebContentsDebuggingEnabled(true);
        dwebView.addJavascriptObject(new JsApi(this), null);
        dwebView.addJavascriptObject(new JsEchoApi(),"echo");
        bt_change_url = (Button) findViewById(R.id.bt_change_url);
        String baseurl_js = getIntent().getStringExtra("baseurl_js");
        if (!TextUtils.isEmpty(baseurl_js)) {
            dwebView.loadUrl(baseurl_js);
        }else {
            dwebView.loadUrl("file:///android_asset/js-call-native.html");
        }

//        dwebView.loadUrl("http://cloud.91xxkj.com:9046/yySSO/app/mobile/login/login.html?who=nshis");
//        dwebView.loadUrl("file:///android_asset/nqc/index.html");
        initListener();
    }

    private void initListener() {
        bt_change_url.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new InputDialog(JavascriptCallNativeActivity.this, R.style.dialog, "", new InputDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm, String content) {
                        if (confirm) {

                            Intent intent = new Intent();
                            intent.setClass(JavascriptCallNativeActivity.this, JavascriptCallNativeActivity.class);
                            intent.putExtra("baseurl_js", content);
                            startActivity(intent);
                            finish();
                        }
                        dialog.dismiss();
                    }
                }).setTitle("确认更改地址?").show();
            }
        });
    }
}
